package libnetdb.dm.assoc.base;

import java.util.*;

public class ExtendedInteresting 
        implements RuleIndex, IndexBuilder {
    
    private Litemset givenSet = null;

    static{
        IndexManager manager = IndexManager.getInstance();
        manager.register(new ExtendedInteresting());
    }
    
    public ExtendedInteresting(){        
    }

    public String getName() {
        return "ExtendedInteresting";
    }

    public String getTitle() {
        return "Extended Interesting";
    }
    
    public Litemset getGivenSet(){
        return givenSet;
    }

    public RuleIndex createIndex(){
        return new ExtendedInteresting();
    }

    public double getIndex(Rule rule) {
        Litemset cause = rule.getCause();
        Litemset conse = rule.getConsequence();
        RuleSet ruleSet = rule.getRuleSet();
        Iterator it = ruleSet.decensterIterator(cause);
        double minValue = rule.getInteresting();
        Litemset localGiven = null; 
        while(it.hasNext()){
            Litemset given = (Litemset) it.next();
            double all = given.count, a = cause.count,
                    b = ruleSet.getCount(conse.merge(given)),
                    ab = rule.getItems().count; 
            double tmp = (ab * all) / (a * b);
            if(tmp < minValue){
                minValue = tmp;
                localGiven = given;
            }
        }
        this.givenSet = localGiven;
        return minValue; 
    }
    
    
}
